package com.sc.services;

import com.sc.mapper.FieldMapper;
import com.sc.pojo.BookField;
import com.sc.pojo.Field;
import com.sc.pojo.PageBean;
import com.sc.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @description: 场地管理service层
 * @author: sc
 * @create: 2023-01-02 17:15
 **/
public class FieldServices {

    //创建工厂
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    /**
     *管理员登陆判断
     * @param username
     * @param password
     * @return
     */
    public boolean isLoginAdmin(String username,String password){
        boolean flag=false;
        if(username .equals("admin")&&password.equals("admin")){

            flag = true;
        }
        if(username .equals("admin2")&&password.equals("admin2")){

            flag = true;
        }

        return flag;
    }

    /**
     * 查询所有场地
     * @return
     */
    public List<Field> selectAllField(){
        SqlSession sqlSession = factory.openSession();
        FieldMapper mapper = sqlSession.getMapper(FieldMapper.class);
        List<Field> fields = mapper.selectAll();
        sqlSession.close();
        return fields;
    }

    /**
     * 查询状态为1的所有场地
     * @return
     */
    public List<Field> selectByShowState(){
        SqlSession sqlSession = factory.openSession();
        FieldMapper mapper = sqlSession.getMapper(FieldMapper.class);
        List<Field> fields = mapper.selectByShowState();
        sqlSession.close();
        return fields;
    }

    /**
     * 添加场地
     * @param field
     */
    public void addField(Field field){
        SqlSession sqlSession = factory.openSession();
        FieldMapper mapper = sqlSession.getMapper(FieldMapper.class);
        mapper.addField(field);
        sqlSession.commit();
        sqlSession.close();
    }
    /**
     * 通过id查询场地
     * @return
     */
    public Field selectFieldById(int fieldid){
        SqlSession sqlSession = factory.openSession();
        FieldMapper mapper = sqlSession.getMapper(FieldMapper.class);
        Field field = mapper.selectById(fieldid);
        sqlSession.close();
        return  field;

    }
    /**
     * 通过ID部分更新场地信息
     * @param field
     */
    public void updateField(Field field){
        SqlSession sqlSession = factory.openSession();
        FieldMapper mapper = sqlSession.getMapper(FieldMapper.class);
        mapper.updateField(field);
        sqlSession.commit();
        sqlSession.close();
    }

    /**
     * 通过ID批量删除场地信息
     * @param ids
     */
    public void deleteFieldByIds(int[] ids){
        SqlSession sqlSession = factory.openSession();
        FieldMapper mapper = sqlSession.getMapper(FieldMapper.class);
        mapper.deleteByIds(ids);
        sqlSession.commit();
        sqlSession.close();
    }
    /**
     * 通过ID删除单个场地信息
     * @param fieldid
     */
    public void deleteFieldById(int fieldid){
        SqlSession sqlSession = factory.openSession();
        FieldMapper mapper = sqlSession.getMapper(FieldMapper.class);
        mapper.deleteById(fieldid);
        sqlSession.commit();
        sqlSession.close();
    }
    //判断是否在时间范围内
    public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
        //设置当前时间
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);
        //设置开始时间
        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);
        //设置结束时间
        Calendar end = Calendar.getInstance();
        end.setTime(endTime);
        //处于开始时间之后，和结束时间之前的判断
        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }
    //判断是否在时间范围内
    public boolean isTheRightTime(BookField bookField) throws ParseException {
        Date today=new Date();
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式
        SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String[] strings = f.format(today).split(" ");

        String nowday = strings[0];
        String nowttime = strings[1];
        System.out.println(nowday+" "+nowttime);
        String bookday = bookField.getBookday();
        String starttime = bookField.getStarttime();
        String endtime = bookField.getEndtime();
        Date nowTime = df.parse(nowttime);
        Date startTime = df.parse(starttime);
        Date endTime = df.parse(endtime);
        boolean flag = false;

        if(belongCalendar(nowTime,startTime,endTime)&&nowday.equals(bookday)){
            flag = true;
        }
        return flag;



    }
    /**
     * 用户在规定时间入场后修改场地状态
     * @param bookField
     */
    public void reviseFieldState(BookField bookField){
        SqlSession sqlSession = factory.openSession();
        FieldMapper mapper = sqlSession.getMapper(FieldMapper.class);
        mapper.updateFieldState(bookField);
        sqlSession.commit();
        sqlSession.close();

    }


    /**
     * 用户在规定时间退场后修改场地状态
     * @param bookField
     */
    public void reviseFieldState1(BookField bookField){
        SqlSession sqlSession = factory.openSession();
        FieldMapper mapper = sqlSession.getMapper(FieldMapper.class);
        mapper.updateFieldStateQuit(bookField);
        sqlSession.commit();
        sqlSession.close();

    }

    /**
     * 根据分页获取page对象
     * @param currentPage
     * @param pageSize
     * @return
     */
    public  PageBean selectByPage(int currentPage,int pageSize){
        SqlSession sqlSession = factory.openSession();
        FieldMapper mapper = sqlSession.getMapper(FieldMapper.class);
        List<Field> fields = mapper.selectByPage((currentPage - 1) * pageSize, pageSize);
        int totalCount = mapper.selectTotalCount();
        return new PageBean(totalCount,fields);
    }


}